using UnityEngine;
using UnityEngine.UI;

namespace JEngine.Core
{
    public class UpdateScreen : MonoBehaviour, IUpdater
    {
        public Button buttonStart;
        public Slider progressBar;
        public Text progressText;
        public Text version;
        public string sceneName;

        private void Start()
        {
            try
            {
                version.text = "资源版本号: v" + Application.version;
            }
            catch
            {
                version.text = "初始版本";
            }
        }

        #region IUpdateManager implementation

        public void OnStart()
        {
            buttonStart.gameObject.SetActive(false);
        }

        public void OnMessage(string msg)
        {
            progressText.text = msg;
        }

        public void OnProgress(float progress)
        {
            progressBar.value = progress;
        }

        public void OnVersion(string ver)
        {
            version.text = $"资源版本号: {ver}";
        }

        public async void OnUpdateFinish(bool result)
        {
            progressBar.gameObject.SetActive(false);
            buttonStart.gameObject.SetActive(!result);
            if (result)
            {
                await AssetMgr.LoadSceneAsync(sceneName);
                Debug.Log("成功进入热更场景");
                await InitJEngine.Instance.LoadHotUpdateCallback();
            }
        }

        #endregion
    }
}